<template>
  <div>
    <tablePaging :tableItem="tableItem" :operationItem="operationItem" />
  </div>
  <fileImport v-model:importVisible="importVisible" :importItem="importItem" @onSuccess="onSuccess" @onError="onError" />
</template>
<script setup>
import tablePaging from '@/views/common/component/table-paging'
import fileImport from '@/views/common/component/fileImport'
import { onMounted, reactive, ref } from 'vue'
import {
  tableItem,
  operationItem
} from './const.js'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import {listDictItemByCode} from '@/api/config/dic'
import {delOffline, publishOffline} from '@/api/resource/offline'
import {getToken} from '@/utils/auth'
import {listTeacher} from '@/api/teacher'
const headers = ref({ Authorization: "Bearer " + getToken() });
const { proxy } = getCurrentInstance();
const router = useRouter()
const importVisible = ref(false)
const importItem = reactive({
  downUrl: '/admin/resource/course/offline/download',//文件下载地址
  upUrl: '/admin/resource/course/offline/import',//文件上传地址
  blob: true,//二进制文件下载
  fileDown() { },//自定义文件下载方法 如果传入下载地址会失效 如果都不传会返回一个fileDown函数 自己@fileDown接收
  downTitle: '',//下载标题 默认1、模板下载
  downButtonText: '',//下载按钮 下载
  upButtonText: '',//上传按钮 上传
  upTitle: '',//上传标题 默认2、表格上传
  downName: '线下课模版'
})
const onSuccess = (file) => {
  tableItem.pageInfo.paging()
}
const onError = (file) => { }
function importHandle() {
  importVisible.value = true
}

function exportHandle() {
  proxy.download("/admin/resource/course/offline/export", tableItem.pageInfo.request, `线下课列表.xlsx`);
}
function initTableItem() {
  // 重置
  operationItem.menuArray[0].handle = () => {
    resetSearch()
  }
// 查询
  operationItem.menuArray[1].handle = () => {
    search()
  }
// 批量删除
  operationItem.menuArray[2].handle = () => {
    batchDelHandle()
  }
  // 新增
  operationItem.menuArray[3].handle = () => {
    addHandle()
  }
// 导入
  operationItem.menuArray[4].handle = () => {
    importHandle()
  }
// 导出
  operationItem.menuArray[5].handle = () => {
    exportHandle()
  }
//详情
  tableItem.btnArray[0].handle = (row) => {
    goDetail(row)
  }
//修改
  tableItem.btnArray[1].handle = (row) => {
    editHandle(row)
  }
//上架
  tableItem.btnArray[2].handle = (row) => {
    publishHandle(1, row)
  }
//下架
  tableItem.btnArray[3].handle = (row) => {
    publishHandle(0, row)
  }
}
function initRequest() {
  tableItem.pageInfo.request = {
    'name': '', //资料名称
    'teacherId': '', //老师id
    'courseType': '', //课程类型,字典表id,内容为市政,经济等
    'publishedFlag': '', //是否上架
    'startTime': '',
    'endTime': '',
    'pageDomain': {
      'pageNum': '1',//分页页面数
      'pageSize': '10',//分页页面数据大小
      'orderByColumn': '',//搜索参数
      'isAsc': '',//搜索参数
      'reasonable': ''//搜索参数
    }
  }
}

function search() {
  tableItem.pageInfo.paging()
}
function resetSearch() {
  initRequest()
  search()
}
function goDetail(row) {
  router.push({
    path: '/modules/resource/offline/detail',
    query: { id: row.id}
  })
}
function addHandle() {
  router.push({
    path: '/modules/resource/offline/add',
    query: { type: 'add' }
  })
}
function editHandle(row) {
  router.push({
    path: '/modules/resource/offline/add',
    query: { id: row.id, type: 'edit' }
  })
}
const selectedRow = ref([])
tableItem.selectChange = (row) => {
  //构建需要删除的id列表
  selectedRow.value = row.map( (item) => {
    return item.id
  } )
}
//批量删除
function batchDelHandle() {
  if(selectedRow.value.length < 1) {return ElMessage.warning('请先选择课程');}
  proxy.$modal.confirm('确认要删除选中的课程吗?').then(function () {
    return delOffline(selectedRow.value);
  }).then(res => {
    tableItem.pageInfo.paging()
    ElMessage.success(res.msg);
  }).catch(() => { });
}
//上下架
function publishHandle(publishedFlag, row) {
  let dataRequest = {
    id: row.id,
    publishedFlag: publishedFlag
  }
  publishOffline(dataRequest).then(res => {
    if (res.code === 200) {
      tableItem.pageInfo.paging()
      ElMessage.success('操作成功');
    }
  })
}
const initOptions = () => {
  operationItem.SearchItem[1].options = []
  operationItem.SearchItem[2].options = []
  listDictItemByCode({typeCode: 'courseTypeCode'}).then(res => {
    const courseList = res.rows
    courseList.map(item => {
      operationItem.SearchItem[1].options.push(item)
    })
  })
  listTeacher({}).then(res => {
    const teacherList = res.rows
    teacherList.map(item => {
      operationItem.SearchItem[2].options.push({ label: item.name, value: item.id })
    })
  })
}

onMounted(() => {
  initTableItem()
  initRequest()
  initOptions()
})
</script>

<style></style>
